﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using TinyLib.Input;

namespace TinyLib.Screen
{
    public class ScreenManager : DrawableGameComponent
    {
        public Dictionary<string, Screen> screens = new Dictionary<string, Screen>();
        SpriteBatch spritebatch = null;

        InputManager inputManager = null;
        public Screen currentScreen = null;

        // le temp

        #region Properties
        public SpriteBatch Spritebatch
        {
            get { return spritebatch; }
        }

        public InputManager InputManager
        {
            get { return inputManager; }
        }

        public string CurrentScreen
        {
            //get { return currentScreen; }
            set
            {
                if (currentScreen != null)
                {
                    currentScreen = screens[currentScreen.GetNextScreen()];
                    if (currentScreen == null)
                    {
                        Game.Exit();
                    }
                    else
                    {
                        currentScreen.Initialize();
                    }
                }
                else
                {
                    screens.TryGetValue(value, out currentScreen);
                    if (currentScreen == null)
                    {
                        Game.Exit();
                    }
                    else
                    {
                        currentScreen.Initialize();
                    }
                }
            }
        }
        #endregion

        public ScreenManager(Game game)
            : base(game)
        { }

        public override void Initialize()
        {
            inputManager = new InputManager(Game);
            inputManager.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spritebatch = new SpriteBatch(GraphicsDevice);
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            inputManager.Update(gameTime);

            if (currentScreen != null)
            {
                currentScreen.HandleInput(ref inputManager);
                currentScreen.Update(gameTime);
                if (currentScreen.changeState)
                {
                    Screen nextState = null;
                    screens.TryGetValue(currentScreen.GetNextScreen(), out nextState);
                    if (nextState != null)
                    {
                        currentScreen.Shutdown();
                        currentScreen = nextState;
                        currentScreen.Initialize();
                    }
                    else
                    {
                        Game.Exit();
                    }
                }
            }

            // base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //Spritebatch.Begin();

            if (currentScreen != null)
            {
                currentScreen.Draw(gameTime);
            }
            base.Draw(gameTime);

            //Spritebatch.End();
        }

        public void AddScreen(string id, Screen screen)
        {
            screens.Add(id, screen);
        }

        public void RemoveScreen(string id)
        {
            Screen screen = screens[id];
            if (screen != null)
            {
                screen.Dispose();
                screens.Remove(id);
            }
        }
    }
}
